/**
 * New node file
 */
module.exports={
	/**
	 * 获取cookie值
	 * @param req
	 * @param name
	 * @returns value 与name相应的value，如果找不到返回false;
	 */
	get	: function(req,name){
		var cookies = {};
		var cookie = req.headers.cookie;
		if(!cookie){
			return false;
		}
		var list = cookie.split(';');
		for (var i=0;i<list.length;i++){
			var pair = list[i].split('=');
			cookies[pair[0].trim()] = pair[1];
		}
		if(cookies[name]){
			return cookies[name];
		}else{
			return false;
		}
	},
	/**
	 * 设置cookie
	 * @param res
	 * @param name
	 * @param val
	 * @param opt
	 */
	set	: function(res,name,val,opt){
		var pairs = [name + '=' + val];
		opt = opt || {};
		
		if(opt.maxAge) pairs.push('Max-Age='+opt.maxAge);
		if(opt.domain) pairs.push('Max-Age='+opt.domain);
		if(opt.path) pairs.push('Max-Age='+opt.path);
		if(opt.expires) pairs.push('Max-Age='+opt.expires);
		if(opt.httpOnly) pairs.push('Max-Age='+opt.httpOnly);
		if(opt.secure) pairs.push('Max-Age='+opt.secure);
		
		res.setHeader('Set-Cookie', pairs.join('; '));
	}
}